package com.mall.server.service.sms;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.api.PmsProductApi;
import com.mall.dubbo.api.SmsFlashPromotionApi;
import com.mall.dubbo.api.SmsFlashPromotionProductRelationApi;
import com.mall.model.domain.PmsProduct;
import com.mall.model.domain.SmsFlashPromotionProductRelation;
import com.mall.model.vo.CommonPage;
import com.mall.model.vo.SmsFlashPromotionProductRelationVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SmsFlashPromotionRelationService {

    @DubboReference
    private SmsFlashPromotionProductRelationApi smsFlashPromotionProductRelationApi;

    @DubboReference
    private PmsProductApi pmsProductApi;

    @DubboReference
    private SmsFlashPromotionApi smsFlashPromotionApi;


    //分页查询不同场次关联及商品信息
    public CommonPage<SmsFlashPromotionProductRelationVo> list(Integer pageNum, Integer pageSize, SmsFlashPromotionProductRelation flashPromotionProductRelation) {

        Long flashPromotionId = flashPromotionProductRelation.getFlashPromotionId();
        Long flashPromotionSessionId = flashPromotionProductRelation.getFlashPromotionSessionId();

        Page<SmsFlashPromotionProductRelation> pageInfo = smsFlashPromotionProductRelationApi.findBySms(flashPromotionId, flashPromotionSessionId, pageNum, pageSize);
        List<SmsFlashPromotionProductRelation> list = pageInfo.getRecords();
        if (CollUtil.isEmpty(list)) {
            return new CommonPage<>();
        }
        List<Long> ids = CollUtil.getFieldValues(list, "productId", Long.class);

        List<PmsProduct> proList = pmsProductApi.findByIds(ids);

        List<SmsFlashPromotionProductRelationVo> vos = new ArrayList<>();
        for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : list) {
            for (PmsProduct pmsProduct : proList) {
                if (pmsProduct != null && pmsProduct.getId().equals(smsFlashPromotionProductRelation.getProductId())) {
                    SmsFlashPromotionProductRelationVo vo = SmsFlashPromotionProductRelationVo.init(smsFlashPromotionProductRelation, pmsProduct);
                    vos.add(vo);
                }
            }
        }
        Page<SmsFlashPromotionProductRelationVo> page = new Page<>();
        BeanUtils.copyProperties(pageInfo, page, "records");
        page.setRecords(vos);


        return new CommonPage<SmsFlashPromotionProductRelationVo>(page);
    }

    //修改关联信息
    public void update(Integer id, SmsFlashPromotionProductRelation relation) {

        smsFlashPromotionProductRelationApi.updateRelation(id, relation);

    }


    public void delete(Integer id) {

        smsFlashPromotionProductRelationApi.removeById(id);
    }


    //批量选择商品添加关联
    public void create(List<SmsFlashPromotionProductRelation> relationList) {

        smsFlashPromotionProductRelationApi.saveBatch(relationList);

    }

    //获取管理商品促销信息
    public SmsFlashPromotionProductRelation findById(Integer id) {

        return smsFlashPromotionProductRelationApi.getById(id);
    }

}
